package com.fanbuchi.fukua.api;

import android.os.Build;

import com.fanbuchi.fukua.application.SuperApp;
import com.fanbuchi.fukua.config.AppConfig;
import com.fanbuchi.fukua.manager.OauthManager;
import com.fanbuchi.fukua.utils.PersistentCookieStore;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;

import java.net.CookieManager;
import java.net.CookiePolicy;

/**
 * Created by killerzhangsan on 2015/5/20.
 * @title host 测试
 * @title host 云测试
 * @title host 正式
 * @title onBackPressed 主页 连续5次点击召唤环境设置页面
 */
public class API {

    private static String hosttest="http://172.16.20.75:8080/v1";

    private static String hostcloud="http://www.fanbuchi.com:8082/v1";

    private static String hostrelease="http://api.fanbuchi.com:80/v1";

    private static String host=hosttest;

    public   static String getHost() {
        if (AppConfig.DEBUG){
            host=hosttest;
        }
        return host;
    }

    protected static OkHttpClient mClient = new OkHttpClient();
    static String client_id="28e7d5d7-b58d-44b9-8716-70e77a604ccb";


    protected   Headers getNormalHeaders(){
        Headers.Builder handers = new Headers.Builder();
            handers .add("User-Agent", Build.MODEL);
            handers.add("client_id", client_id);
         mClient.setCookieHandler(new CookieManager(new PersistentCookieStore(SuperApp.getApplication()), CookiePolicy.ACCEPT_ALL));
        return handers.build();
    }


    public  void normalPostRequest(FormEncodingBuilder builder,Callback callback,String url) {
        RequestBody formBody =  builder.build();
        Request request = new Request.Builder()
                .url(url)
                .headers(getNormalHeaders())
                .post(formBody)
                .build();
        mClient.newCall(request).enqueue(callback);
    }

    public  void normalGetRequest(Callback callback,String url) {
        Request request = new Request.Builder()
                .url(url)
                .headers(getNormalHeaders())
                .build();
        mClient.newCall(request).enqueue(callback);
    }

}
